package com.bss.ajax;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

import com.bss.dao.ConnectionDao;
import com.bss.framework.FrontController;

public class BackupCommand implements AjaxCommand {

	private static final String BACKUPTARGET = "backuptarget.txt";
	public static final String BACKUP_FILE = "D:\\BSS-Backup\\Backup";

	public String execute(Map<String, String[]> input) {
		try {
			FrontController.debug("Backup Started");
			String status = backup();
			FrontController.debug("Backup done..." + status);
			return "Backup done successfully...: Status: " + status;
		} catch (Exception e) {
			FrontController.debug("Backup Failed: " + e.getMessage());
			return "ERROR: " + e.getMessage();
		}
	}

	public String fetchCommand() {
		return "mysqldump.exe";
	}

	public String backup() throws Exception {

		String command = fetchCommand();

		if (command == null)
			throw new Exception("ERROR: Backup command not found...");

		String targets[] = logFiles();

		if (targets == null) {

			String target = BACKUP_FILE + "-"
			+ getDate() + ".txt";
			
			String exec = command + " -u root --password=" + ConnectionDao.PASSWORD
					+ " --lock-tables=FALSE -r " +target +" "+ ConnectionDao.DATABASE;

			Process pp = Runtime.getRuntime().exec(exec);
			pp.waitFor();
			
			return "Backup done here: "+target;

		} else {

			String targetNames[] = new String[targets.length];
			for (int i=0;i<targets.length;i++) {
				String targetName = targets[i] + "-" + getDate() + ".txt";
				String exec = command + " -u root --password="
						+ ConnectionDao.PASSWORD + " --lock-tables=FALSE -r "
						+ targetName +" "
						+ ConnectionDao.DATABASE;

				Process pp = Runtime.getRuntime().exec(exec);
				pp.waitFor();
				targetNames[i] = targetName;
			}
			return "Backup done here: "+Arrays.toString(targetNames);
		}
	}

	public static String getDate() {
		Calendar c = new GregorianCalendar();
		return c.get(Calendar.DATE) + "-" + (c.get(Calendar.MONTH) + 1) + "-"
				+ c.get(Calendar.YEAR);
	}

	class StreamReader implements Runnable {

		private InputStream iis;

		public StreamReader(InputStream iis) {
			this.iis = iis;
		}

		public void run() {
			BufferedReader br = new BufferedReader(new InputStreamReader(iis));
			String str = null;
			try {
				while ((str = br.readLine()) != null) {
					System.out.println(str);
				}
			} catch (IOException e) {
			}
		}
	}

	public String[] logFiles() {

		ArrayList<String> files = new ArrayList<String>();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(this
					.getClass().getResourceAsStream(BACKUPTARGET)));
			String str = null;
			while ((str = br.readLine()) != null) {
				System.out.println(str);
				files.add(str);
			}
			return files.toArray(new String[0]);
		} catch (Exception e) {
		}
		return null;
	}

	public static void main(String[] args) {
		new BackupCommand().execute(null);
	}

}
